package newx.com.control.text
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	import newx.com.ResManager;
	import newx.com.control.ITextInput;
	
	/**
	 * 单行带背景的输入文本框，目标低耗
	 */
	public class NxInput extends NxText implements ITextInput
	{
		private const defaultWidth		:Number=40;
		private const defaultHeight	:Number=40;
		private const defaultColor		:uint=0xFFFFFF;
		
		private var _htmlStr			:String	=	"";
		private var _bg:Sprite;
		
//		private var _left				:uint	=	0;
		private var _offsetX			:uint	=	0;
		private var _offsetY			:uint	=	0;
		private var _skinWidth			:uint	=	200;
		private var _skinHeight		:uint	=	200;
		
		private var _skin				:String		=	"";
		private var _condenseWhite		:Boolean	=	false;
		private var _autoSkinScale		:Boolean	=	false;
		
		private var _color				:uint		=	0xa1d5fd;
		
		/**
		 * 
		 * @param w 文本区域宽度
		 * @param h 文本区域高度
		 * @param _skin 皮肤String
		 * @param l 左偏移
		 * @param t 上偏移
		 * @param c 颜色
		 */
		public function NxInput()
		{		
			_bg=new Sprite();
			addChild(_bg);
//			addChild(_textField);
			
			super();
			init();
		}
		
		private function init():void
		{
			_textField.type			=	TextFieldType.INPUT;
			
			_textField.selectable 	= 	true;
			
			_textField.width		=	_skinWidth+_offsetX;
			_textField.height		=	_skinHeight+_offsetY;
			_textField.textColor	=	_color;
			
			_textField.addEventListener(Event.CHANGE, onChange);
			addEventListener(MouseEvent.CLICK,focusHandler);
		}
				
//		/**遗留接口*/
//		public function set top(value:uint):void
//		{
//			_top = value;
////			_inputTxt.height	-=	value;
//			_inputTxt.y	=	value;
//		}
//		
//		/**遗留接口*/
//		public function get top():uint
//		{
//			return _top;
//		}

//		/**
//		 * 左偏移量设置
//		 * @param value
//		 */
//		public function set left(value:uint):void
//		{
//			_left = value;
////			_inputTxt.width	-=	value;
//			_inputTxt.x	=	value;
//		}
//
//		public function get left():uint
//		{
//			return _left;
//		}
//		
//		/**
//		 * 
//		 * @param value
//		 */
//		public function set multiline(value:Boolean):void
//		{
//			_textField.multiline = value;
//		}
//		
//		/**
//		 * 一个布尔值，指示字段是否为多行文本字段。
//		 * */
//		public function get multiline():Boolean
//		{
//			return _textField.multiline;
//		}

//		public function setHtmlStr(value:String):void
//		{
//			_textField.htmlText = value;
//		}
		/**老接口*/
		public function set txtColor(val:uint):void
		{
			_textField.textColor	=	val;
		}
		
		public function get maxChars():int 
		{
			return _textField.maxChars;
		}
		
		public function set maxChars(value:int):void 
		{
			_textField.maxChars = value;
		}
		
		public function set restrict(value:String):void
		{
			_textField.restrict=value;
		}
		
		public function get restrict():String
		{
			return _textField.restrict;
		}
		
		public function set textFormat(textFormat:TextFormat):void
		{
			NxTextFormatManager.setFormatToOld(textFormat,_format);
			NxTextFormatManager.updataTextFormat(_textField,_format);
		}
		
		//使用滤镜
		public function useFilters():void
		{
			var filter:BitmapFilter 		= 	getGlowFilter();
			var myFilters:Array 			= 	[];
			myFilters.push(filter);
			_textField.filters = myFilters;
		}
		
		private function getGlowFilter():GlowFilter
		{
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	4;
			glow.quality			= 	BitmapFilterQuality.HIGH;
			return glow;
		}
		
		/**
		 * 文本背景皮肤，参数为资源绑定类类名。
		 * */
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(val:String):void
		{
			_skin=val;
			
			while(_bg.numChildren>0)
			{
				_bg.removeChildAt(0);
			}
			
			var bgSkin:Sprite	=	ResManager.newResClass(_skin) as Sprite;
			
			if(bgSkin)
			{
				_bg.addChild(bgSkin);
				
				if(_autoSkinScale)
				{
					bgSkin.width=_offsetX+_textField.width;
					bgSkin.height=_offsetY+_textField.height;
				}
				else
				{
					bgSkin.width	=	_skinWidth;
					bgSkin.height	=	_skinHeight;
				}
			}
		}
		
		/**
		 * 输入框背景宽度（如果有）。
		 * */
		public function get skinWidth():uint
		{
			return _skinWidth;
		}
		public function set skinWidth(val:uint):void
		{
			if(_skinWidth==val)	return;
			
			_skinWidth=val;
			
			updateDisplay();
		}
		
		override public function get width():Number
		{
			return _offsetX+_textField.width;
		}
		
		override public function get height():Number
		{
			return _offsetY+_textField.height;
		}
		
		/**
		 * 输入框背景高度（如果有）。
		 * */
		public function get skinHeight():uint
		{
			return _skinHeight;
		}
		
		public function set skinHeight(val:uint):void
		{
			if(_skinHeight==val)	return;
			
			_skinHeight=val;
			
			updateDisplay();
		}
		
		/**
		 * 指定是否应删除包含 HTML 文本的 TextInput 控件中的额外空白（空格、换行符等）。<>klk<br>lkl<>
		 * */
		public function get condenseWhite():Boolean
		{
			return _textField.condenseWhite;
		}
		
		public function set condenseWhite(val:Boolean):void
		{
			_textField.condenseWhite=val;
		}
		
		/**
		 * 一个布尔值，是否根据文本大小缩放背景皮肤（如果有）
		 * */
		public function get autoSkinScale():Boolean
		{
			return _autoSkinScale;
		}
			
		public function set autoSkinScale(val:Boolean):void
		{
			if(_autoSkinScale==val)	return;
			
			_autoSkinScale=val;
			
			updateDisplay();
			
			if(_autoSkinScale)
			{
				_textField.addEventListener(Event.CHANGE,inputHandler);
			}
			else
			{
				_textField.removeEventListener(Event.CHANGE,inputHandler);
			}
		}
		
		private function inputHandler(e:Event):void
		{
			updateDisplay();
		}
		
		private function focusHandler(e:MouseEvent):void
		{
			stage.focus	=	_textField;
		}
		
		/**
		 * 一个布尔值，指示此控件是否用于输入密码。
		 * */
		public function get displayAsPassword():Boolean
		{
			return _textField.displayAsPassword;
		}
		public function set displayAsPassword(val:Boolean):void
		{
			_textField.displayAsPassword=val;
		}
		
		/**
		 * 一个布尔值，指示用户是否可以编辑此控件中的文本。
		 * */
		public function get editable():Boolean
		{
			var result:Boolean;
			
			if(TextFieldType.INPUT==_textField.type)
			{
				result=true;	
			}
			else
			{
				result=false;
			}
			
			return result;
		}
		public function set editable(val:Boolean):void
		{
			if(val)
			{
				_textField.type=TextFieldType.INPUT;
			}
			else
			{
				_textField.type=TextFieldType.DYNAMIC;
			}
		}
		
		public function set offsizeX(val:int):void
		{
			if(_offsetX==val)	return;
			
			_offsetX = val;
			
			updateDisplay();
		}

		public function get offsizeX():int	
		{
			return _offsetX;
		}
			
		public function set offsizeY(val:int):void
		{
			if(_offsetY==val)	return;
			
			_offsetY=val;
			
			updateDisplay();
		}
		
		public function get offsizeY():int
		{
			return _offsetY;
		}

		public function get length():int
		{
			return _textField.length;
		}
		
		public function get strByteSize():int
		{
			var byte:ByteArray = new ByteArray();
			byte.writeMultiByte(_textField.text, "cn-gb");
			var length:uint = byte.length;
			
			return length;
		}
		
//		public function focused():void
//		{
//			if(	null	!=	stage)
//				stage.focus	=	_textField;
//		}

		private function updateDisplay():void
		{
			if(_autoSkinScale)
			{
				_textField.autoSize=TextFieldAutoSize.LEFT;
				
				if(_bg.numChildren>0)
				{
					_bg.getChildAt(0).width=this._textField.width+_offsetX*2;
					_bg.getChildAt(0).height=this._textField.height+_offsetY*2;
				}
				
				_textField.x=_offsetX;
				_textField.y=_offsetY;
			}
			else
			{
				if(_bg.numChildren>0)
				{
					_bg.getChildAt(0).width=_skinWidth;
					_bg.getChildAt(0).height=_skinHeight;
				}
				_textField.autoSize=TextFieldAutoSize.LEFT;
				_textField.width=_skinWidth-_offsetX*2;
				_textField.height=_skinHeight-_offsetY*2;
				
				_textField.x=_offsetX;
				_textField.y=_offsetY;
			}
		}
		
		private function onChange(evt : Event):void
		{
			updataScrollPanel();
		}
	}
}